#pragma once

#include <iostream>
#include <vector>
#include <string>
#include <jsoncpp/json/json.h>

#include "../comm/util.hpp"
#include "../comm/log.hpp"
#include "./include/mysql.h"
namespace ns_login_register
{
    using namespace ns_logUtil;
    const std::string oj_questions = "user";
    const std::string host = "127.0.0.1";
    const std::string user = "oj_client";
    const std::string passwd = "Th18768696673++";
    const std::string db = "oj";
    const int port = 3306;

    // 存储用户的信息
    struct User{
        std::string user_name;
        std::string passwd;
        std::string session;
        int max_age;
        std::vector<int> Success;//提交正确的题目编号
    };
    class Register
    {
    public:
        Register()
        {};
        ~Register()
        {}
    public:
        bool QueryMySql_out(const std::string &sql, std::vector<User> *out)
        {
            // 创建mysql句柄
            MYSQL *my = mysql_init(nullptr);

            // 连接数据库
            if(nullptr == mysql_real_connect(my, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0))
            {
                LOG(FATAL) << "连接数据库失败..." << std::endl;
                return false;
            }

            // 一定要设置编码格式，否则会出现乱码
            mysql_set_character_set(my, "utf8");

            LOG(INFO) << "连接数据库成功" << std::endl;

            // 执行sql语句
            if(0 != mysql_query(my, sql.c_str()))
            {
                LOG(WARNING) << sql << " execute error!" << std::endl;
                return false;
            }

            // 提取结果
            MYSQL_RES *res = mysql_store_result(my);
            
            // 分析结果
            int rows = mysql_num_rows(res); // 获得行数
            int cols = mysql_num_fields(res); // 获得列数

            User u;

            for(int i = 0; i < rows; i++)
            {
                MYSQL_ROW row = mysql_fetch_row(res);
                u.user_name = row[0];
                u.passwd = row[1];
                out->push_back(u);
            }

            free(res);
            mysql_close(my);

            return true;
        }
        
        bool QueryMySql_in(const std::string &sql)
        {
            // 创建mysql句柄
            MYSQL *my = mysql_init(nullptr);

            // 连接数据库
            if(nullptr == mysql_real_connect(my, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0))
            {
                LOG(FATAL) << "连接数据库失败..." << std::endl;
                return false;
            }

            // 一定要设置编码格式，否则会出现乱码
            mysql_set_character_set(my, "utf8");

            LOG(INFO) << "连接数据库成功" << std::endl;

            // 执行sql语句
            if(0 != mysql_query(my, sql.c_str()))
            {
                LOG(WARNING) << sql << " execute error!" << std::endl;
                return false;
            }

            
            mysql_close(my);

            return true;
        }
        
        void register1(std::string& in_json)
        {
            Json::Value v;
            Json::Reader reader;

            // Parse JSON input
            if (!reader.parse(in_json, v))
            {
                LOG(ERROR) << "Failed to parse JSON: " << reader.getFormattedErrorMessages();
                return;
            }

            std::string username = v["username"].asString();
            std::string password = v["password"].asString();

            // Insert into MySQL
            std::string sql = "INSERT INTO user (username, passwd) VALUES ('" + username + "', '" + password + "')";
            

            if (!QueryMySql_in(sql))
            {
                LOG(ERROR) << "Failed to execute MySQL query.";
                return;
            }
        }

        void getUsername(std::string *out_json)
        {
             std::vector<User> out;
            std::string sql1 = "select * from user";
            std::string s;
            if (!QueryMySql_out(sql1, &out))
            {
                LOG(ERROR) << "Failed to execute MySQL query.";
                return;
            }
            for(int i = 0; i < out.size(); i++)
            {
                s += out[i].user_name;
                s += ";";
            }

            Json::FastWriter writer;
            Json::Value va;
            va["u"] = s;
            *out_json = writer.write(va);
        }
        
        
    };
}